//
//  RBStockAddOrder2VC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/17.
//

#import "RBStockAddOrder2VC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "RBTextField.h"
#import "RBTextView.h"
#import "RBUploadTool.h"
#import "RBGrowablePhotoContainer.h"
#import "RBStockAddOrderSubmitVC.h"

@interface RBStockAddOrder2VC ()

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) RBTextField *companyNameF;
@property (nonatomic, strong) RBTextField *companyAddressF;
@property (nonatomic, strong) RBTextField *doctorNameF;
@property (nonatomic, strong) RBTextField *phoneF;
@property (nonatomic, strong) RBTextField *nameF;
@property (nonatomic, strong) RBTextField *sexF;
@property (nonatomic, strong) RBTextView *markF;
@property (nonatomic, strong) RBGrowablePhotoContainer *photoView;
@property (nonatomic, strong) RBUploadTool *uploadTool;

@end

@implementation RBStockAddOrder2VC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self fillData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"填写基础信息";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:[self goodsView] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view view_addView:[RanTool labelWithFontSize:Zoom(14) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"基础信息"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
        [view view_addView:({
            UIView *view = [self itemWithTitle:@"医疗机构名称" placeholder:@"请填写医疗机构名称"];
            self.companyNameF = (RBTextField *)[view viewWithTag:333];
            self.companyNameF.text = self.allModel.user_detail.company_name;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(12) bottom:0 left:0 right:0 centerX:0];
        [view view_addView:({
            UIView *view = [self itemWithTitle:@"医疗机构地址" placeholder:@"请填写医疗机构地址"];
            self.companyAddressF = (RBTextField *)[view viewWithTag:333];
            NSString *addressString = [NSString stringWithFormat:@"%@ %@ %@ %@", self.allModel.user_detail.province, self.allModel.user_detail.city, self.allModel.user_detail.addr_detail, self.allModel.user_detail.district];
            self.companyAddressF.text = addressString;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:({
            UIView *view = [self itemWithTitle:@"医生姓名" placeholder:@"请填写医生姓名"];
            self.doctorNameF = (RBTextField *)[view viewWithTag:333];
            self.doctorNameF.text = self.allModel.user_detail.name;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:({
            UIView *view = [self itemWithTitle:@"手机号" placeholder:@"请填写手机号"];
            RBTextField *textF = (RBTextField *)[view viewWithTag:333];
            textF.type = RBTextFieldTypePhone;
            self.phoneF = textF;
            self.phoneF.text = self.allModel.user_detail.mobile;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:({
            UIView *view = [self itemWithTitle:@"患者姓名" placeholder:@"请填写患者姓名"];
            self.nameF = (RBTextField *)[view viewWithTag:333];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:({
            UIView *view = [self itemWithTitle:@"患者性别" placeholder:@"请选择性别"];
            RBTextField *textF = (RBTextField *)[view viewWithTag:333];
            textF.userInteractionEnabled = NO;
            [view rbAddTapGesWithTarget:self action:@selector(genderViewTapped)];
            self.sexF = textF;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"医生留言"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(12) bottom:0 left:Zoom(13) right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            [view rbSetCornerRadius:Zoom(5)];
            view.backgroundColor = [RBThemeConfig bgGrayColor];
            [view view_addView:({
                RBTextView *view = RBTextView.new;
                view.backgroundColor = UIColor.clearColor;
                view.font = [UIFont systemFontOfSize:Zoom(13)];
                view.textColor = [RBThemeConfig textBlackColor];
                view.placeHolder = @"请填写医生附言";
                view.placeHolderFontSize = Zoom(13);
                self.markF = view;
                view;
            }) withHeight:Zoom(90) width:CGFLOAT_MIN top:Zoom(13)-7 bottom:Zoom(13)-7 left:Zoom(13)-3 right:Zoom(13)-3 centerX:NO];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(18) bottom:Zoom(24) left:Zoom(13) right:Zoom(13) centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view view_addView:[RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"请上传真实的口腔照片"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:0 left:Zoom(13) right:0 centerX:0];
        
        CGFloat photoW = Zoom(264);
        RBGrowablePhotoContainer *photoView = [RBGrowablePhotoContainer viewWithMaxNum:9 colNum:4 editable:YES viewWidth:photoW];
        self.photoView = photoView;
        [view view_addView:photoView withHeight:photoView.viewHeight width:photoW top:Zoom(13) bottom:Zoom(13) left:Zoom(13) right:CGFLOAT_MIN centerX:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(10) left:Zoom(15) right:Zoom(15) centerX:0];
}

#pragma mark - Network

#pragma mark - Event

- (void)genderViewTapped
{
    NSArray *sexArray = @[@"男", @"女"];
    LCWeakSelf;
    [JKAction showTitle:nil message:nil btnArray:sexArray completion:^(int index) {
        weakSelf.sexF.text = sexArray[index];
    }];
}

- (void)nextBtnTapped
{
    [self.view endEditing:YES];
    NSString *companyNameString = self.companyNameF.text;
    NSString *companyAddressString = self.companyAddressF.text;
    NSString *doctorNameString = self.doctorNameF.text;
    NSString *phoneString = self.phoneF.text;
    NSString *sexString = self.sexF.text;
    NSString *nameString = self.nameF.text;
    NSString *markString = self.markF.text;
    if ([companyNameString isEqualToString:@""]) {
        [self.view showTip:self.companyNameF.placeholder completion:nil];
    } else if ([companyAddressString isEqualToString:@""]) {
        [self.view showTip:self.companyAddressF.placeholder completion:nil];
    } else if ([doctorNameString isEqualToString:@""]) {
        [self.view showTip:self.doctorNameF.placeholder completion:nil];
    } else if ([phoneString isEqualToString:@""]) {
        [self.view showTip:self.phoneF.placeholder completion:nil];
    } else if ([nameString isEqualToString:@""]) {
        [self.view showTip:self.nameF.placeholder completion:nil];
    } else if ([sexString isEqualToString:@""]) {
        [self.view showTip:self.sexF.placeholder completion:nil];
    } else {
        if (self.photoView.photos.count == 0) {
            [self goNextVCWith:@""];
        } else {
            LCWeakSelf;
            [self.uploadTool uploadData:({
                NSMutableArray *arrayM = NSMutableArray.array;
                NSArray *imgs = self.photoView.photos;
                for (UIImage *img in imgs) {
                    RBUploadModel *model = RBUploadModel.new;
                    model.source = img;
                    [arrayM addObject:model];
                }
                arrayM;
            }) withCompletion:^(NSArray *data) {
                NSMutableArray *arrayM = NSMutableArray.array;
                for (RBUploadModel *model in data) {
                    [arrayM addObject:model.res];
                }
                [weakSelf goNextVCWith:[arrayM componentsJoinedByString:@","]];
            }];
        }
    }
}
//下一页
- (void)goNextVCWith:(NSString *)url {
    self.submitModel.institution_name = self.companyNameF.text;
    self.submitModel.institution_address = self.companyAddressF.text;
    self.submitModel.doctor_name = self.doctorNameF.text;
    self.submitModel.doctor_mobile = self.phoneF.text;
    self.submitModel.patient_name = self.nameF.text;
    self.submitModel.patient_sex = self.sexF.text;
    self.submitModel.postscript = self.markF.text;
    self.submitModel.picarr = url;
    
    RBStockAddOrderSubmitVC *vc = RBStockAddOrderSubmitVC.new;
    vc.submitModel = self.submitModel;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIView *)bottomView
{
    if (!_bottomView)
    {
        self.bottomView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.height.mas_equalTo(Zoom(60) + kRBSafeBottomHeight);
            }];
            
            UIStackView *stackView = UIStackView.new;
            stackView.spacing = Zoom(20);
            stackView.axis = UILayoutConstraintAxisHorizontal;
            stackView.distribution = UIStackViewDistributionFillEqually;
            [view addSubview:stackView];
            [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(Zoom(15));
                make.right.mas_equalTo(-Zoom(15));
                make.top.mas_equalTo(Zoom(10));
                make.height.mas_equalTo(Zoom(40));
            }];
            [stackView addArrangedSubview:({
                UILabel *view = [self btnWithTitle:@"下一步" type:1];
                [view rbAddTapGesWithTarget:self action:@selector(nextBtnTapped)];
                view;
            })];
            view;
        });
    }
    return _bottomView;
}

- (UILabel *)btnWithTitle: (NSString *)title type: (NSInteger)type
{
    if (type == 0)
    {
        UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textLightBlackColor] align:1 text:title bgColor:UIColor.whiteColor radius:Zoom(20)];
        [view rbSetBorderWitdh:1 borderColor:[RBThemeConfig textGrayColor]];
        return view;
    }
    else
    {
        return [RanTool labelWithFontSize:Zoom(13) weight:1 color:UIColor.whiteColor align:1 text:title bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(20)];
    }
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.bottom.mas_equalTo(self.bottomView.mas_top);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)goodsView
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    [view rbSetCornerRadius:Zoom(8)];
    
    NSString *orderNumString = [NSString stringWithFormat:@"订单编号：%@", self.submitModel.order_num];
    
    [view view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:orderNumString] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
    
    [view view_addView:({
        UIView *view = UIView.new;
        
        [view h_view_addView:[RanTool imgViewWithMode:2 radius:0 imgName:self.allModel.picurl] withHeight:Zoom(99) width:Zoom(99) top:Zoom(12) bottom:Zoom(12) left:Zoom(12) right:0 centerY:0];
        
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:self.allModel.good_name];
            view.numberOfLines = 3;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(25) bottom:CGFLOAT_MIN left:Zoom(8) right:Zoom(12) centerY:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
    
    return view;
}

- (UIView *)itemWithTitle: (NSString *)title placeholder: (NSString *)placeholer
{
    UIView *view = UIView.new;
    
    [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title] withHeight:CGFLOAT_MIN width:Zoom(100) top:Zoom(12) bottom:Zoom(12) left:Zoom(Zoom(13)) right:0 centerY:0];
    
    RBTextField *textF = RBTextField.new;
    [view h_view_addView:textF withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:Zoom(13) centerY:0];
    textF.tag = 333;
    textF.textAlignment = NSTextAlignmentRight;
    textF.font = [UIFont systemFontOfSize:Zoom(13)];
    textF.textColor = [RBThemeConfig textBlackColor];
    textF.placeholder = placeholer;
    
    return view;
}

- (RBUploadTool *)uploadTool
{
    if(!_uploadTool)
    {
        self.uploadTool = RBUploadTool.new;
    }
    return _uploadTool;
}

@end
